package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Supplier;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISupplierService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 丧彪
 */
@Controller
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;
    
    @RequiresPermissions("supplier:index")
    @PermissionName("供应商首页")
    @RequestMapping("")
    public String index(){
        return "supplier/list";
    }

    @RequiresPermissions("supplier:list")
    @PermissionName("供应商列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo)
    {
        return supplierService.query(qo);
    }
    
    @RequiresPermissions("supplier:save")
    @PermissionName("供应商保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Supplier entity){
    	JsonResult result = new JsonResult();
    	try {
    		supplierService.save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("保存失败" + e.getMessage());
		}
        return result;
    }
    
    @RequiresPermissions("supplier:update")
    @PermissionName("供应商更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Supplier entity){
    	JsonResult result = new JsonResult();
    	try {
    		supplierService.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("更新失败" + e.getMessage());
		}
        return result;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            supplierService.delete(id);
            result.markMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("删除失败");
        }
        return result;
    }

    @RequestMapping("querySupplier")
    @ResponseBody
    public List<Supplier> querySupplier(){
        return supplierService.list();
    }
}
